using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace LS.Tools
{
    public class TimeUpdater
    {
        public float DeltaTime { get; private set; }
        public float RealDeltaTime { get; private set; }
        public float Time { get; private set; }
        public float RealTime { get; private set; }
        public int Seconds { get; private set; }
        public int RealSeconds { get; private set; }

        public event Action<float, float> OnTimeUpdate = null;
        public event Action<int> OnTimeSecondsUpdate = null;
        public event Action<int> OnRealTimeSecondsUpdate = null;

        public void Reset()
        {
            DeltaTime = 0;
            RealDeltaTime = 0;
            Time = 0;
            RealTime = 0;
            Seconds = 0;
            RealSeconds = 0;
        }

        public void Clear()
        {
            Reset();
            OnTimeUpdate = null;
            OnTimeSecondsUpdate = null;
        }

        public void Update(float deltaSeconds,float realDeltaSeconds)
        {
            if (deltaSeconds != 0)
            {
                this.DeltaTime = deltaSeconds;
                this.Time += deltaSeconds;
                int second = Mathf.FloorToInt(Time);
                if (second > Seconds)
                {
                    Seconds = second;
                    OnTimeSecondsUpdate?.Invoke(Seconds);
                }
            }

            this.RealDeltaTime = realDeltaSeconds;
            this.RealTime += realDeltaSeconds;
            int realSecond = Mathf.FloorToInt(RealTime);
            if (realSecond > this.RealSeconds)
            {
                this.RealSeconds = realSecond;
                OnRealTimeSecondsUpdate?.Invoke(RealSeconds);
            }
            OnTimeUpdate?.Invoke(DeltaTime, RealDeltaTime);
        }
    }
}
